﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using InformationExtraction.Common;
using InformationExtraction.Interface;

namespace InformationExtraction.Utils
{
    //lz
    class HomepageExtracter : IFeatureExtractable
    {
        /// <summary>
        /// 是否以HTTP协议头开始
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private int GetHttpFeature(Token token)
        {
            string normal = token.Content.Trim().ToLower();

            Regex reg = new Regex(@"^http://.*", RegexOptions.IgnoreCase);
            
            return reg.Match(normal).Success ? 1 : -1;
        }

        /// <summary>
        /// 是否在上下2行范围内出现fax, phone, mail字样
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private int GetConfusionFeature(Token token)
        {
            Line line = token.InLine;
            for (int i = 0; i < 3 && line != null; i++)
            {
                if (line.RawContent.ToLower().Contains("fax") || line.RawContent.ToLower().Contains("mail")
                    || line.RawContent.ToLower().Contains("phone"))
                    return -1;

                line = line.PriorLine;
            }

            line = token.InLine.NextLine;
            for (int i = 0; i < 2 & line != null; i++)
            {
                if (line.RawContent.ToLower().Contains("fax") || line.RawContent.ToLower().Contains("mail")
                    || line.RawContent.ToLower().Contains("phone"))
                    return -1;

                line = line.NextLine;
            }

            return 1;
        }

        /// <summary>
        /// 是否在上、下2行范围内出现homepage字样
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private int GetHPFeature(Token token)
        {
            string normal = token.Content.Trim().ToLower();

            Regex reg = new Regex(@"^http://.*", RegexOptions.IgnoreCase);
            Match mc = reg.Match(normal);

            if (mc.Success)
            {
                Line line = token.InLine;
                for (int i = 0; i < 2 && line != null; i++)
                {
                    if (line.RawContent.ToLower().Contains("homepage"))
                        return 1;

                    line = line.PriorLine;
                }

                line = token.InLine.NextLine;
                for (int i = 0; i < 1 & line != null; i++)
                {
                    if (line.RawContent.ToLower().Contains("homepage"))
                        return 1;

                    line = line.NextLine;
                }

                return -1;
            }

            return -1; 
        }

        #region IFeatureExtractable 成员

        List<int> IFeatureExtractable.Extract(Token token)
        {
            List<int> vec = new List<int>();

            vec.Add(GetHttpFeature(token));
            vec.Add(GetConfusionFeature(token));
            vec.Add(GetHPFeature(token));

            return vec;
        }

        InformationType IFeatureExtractable.GetTargetType()
        {
            return InformationType.Homepage;
        }

        #endregion
    }
}
